package data;

import data.ConnectionManager;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import utils.UIDGenerator;

import domain.Queja;
import domain.Sugerencia;
import domain.Usuario;

public class JDBCApoyosDAO implements IApoyosDAO {

	private Connection conn;
	
	public JDBCApoyosDAO(){
		conn = ConnectionManager.getInstance().checkOut();
		System.out.println("SE HA CREADO LA CONEXION");
	}
	@Override
	public List<Queja> selectAllQuejas(String OIDUsuario) {
		PreparedStatement stmt = null;
        List searchResults = new LinkedList();
        ResultSet result = null;

        try {

            String sql = "SELECT * FROM  quejas INNER JOIN apoyos ON quejas.OIDQueja = apoyos.quejaOID WHERE (usuarioOID = ?)";
            stmt = conn.prepareStatement(sql); //conn es una conexi�n v�lida
            stmt.setString(1, OIDUsuario);
            stmt.executeQuery();
            result = stmt.executeQuery();

            while (result.next()) {
            	Queja temp = new Queja();
                temp.setIdQueja(result.getString("OIDQueja"));
                
                searchResults.add(temp);
            }
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            ConnectionManager.getInstance().checkIn(conn);
            System.out.println("SE HA CERRADO LA CONEXION");
            try {
                if (result != null)
                    result.close();
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }

        }
        return searchResults;
    }
	
		
	@Override
	public List<Usuario> selectAllUsuarios(String OIDQueja) {
		// TODO Auto-generated method stub
		PreparedStatement stmt = null;
        List searchResults = new LinkedList();
        ResultSet result = null;

        try {

            String sql = "SELECT * FROM  usuarios INNER JOIN apoyos ON usuarios.OIDUsuario = apoyos.usuarioOID WHERE (quejaOID = ?)";
            stmt = conn.prepareStatement(sql); //conn es una conexi�n v�lida
            stmt.setString(1, OIDQueja);
            stmt.executeQuery();
            result = stmt.executeQuery();

            while (result.next()) {
            	Usuario temp = new Usuario();
                temp.setIdUsuario(result.getString("OIDUsuario"));
                temp.setUser(result.getString("user"));
                
                searchResults.add(temp);
            }
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            ConnectionManager.getInstance().checkIn(conn);
            System.out.println("SE HA CERRADO LA CONEXION");
            try {
                if (result != null)
                    result.close();
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }

        }
        return searchResults;
    }
	
	public void insert(Queja q, Usuario u) {
		// TODO Auto-generated method stub
        PreparedStatement stmt = null;
        String sql = "INSERT INTO apoyos ( OIDQueja, OIDusuario) VALUES (?, ?) ";
        try {
            stmt = conn.prepareStatement(sql);

            stmt.setString(1, q.getIdQueja());
            stmt.setString(2, u.getIdUsuario());
            
            stmt.executeUpdate();


        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
        	ConnectionManager.getInstance().checkIn(conn);
        	System.out.println("SE HA CERRADO LA CONEXION");
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }


	}	

}
